package com.hospital.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    //获取默认格式的当前日期及时间
    public static String getDateTime() {
        Date currDate = Calendar.getInstance().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
        String dateTime = sdf.format(currDate);
        return dateTime;
    }

    //获取pattern传入格式的当前日期及时间
    public static String getDateTime(String pattern) {
        Date currDate = Calendar.getInstance().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dateTime = sdf.format(currDate);
        return dateTime;//返回了日期与时间的字符串
    }

    //获取星期几
    public static String getWeek() {
        Calendar calendar = Calendar.getInstance();
        String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        int index = calendar.get(Calendar.DAY_OF_WEEK);
        String week = weeks[index - 1];
        return week;
    }

    //将Date转化为规范的日期函数
    public static String date2Str(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
        return sdf.format(date);
    }

    //将date转换成pattern格式的字符串
    public static String date2Str(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    //将字符串类型转换为时间类型
    public static Date str2Date(String str) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    //将字符串按照pattern格式转换为时间类型
    public static Date str2Date(String str, String pattern) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    // 将时间格式化
    public static Date DatePattern(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
        try {
            String dd = sdf.format(date);
            date = str2Date(dd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    //将时间按照pattern格式格式化
    public static Date DatePattern(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            String dd = sdf.format(date);
            date = str2Date(dd, pattern);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    // 计算两个日期相隔的天数
    public static int nDaysBetweenTwoDate(String starttime, String endtime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date firstDate = null;
        Date secondDate = null;
        try {
            firstDate = df.parse(starttime);
            secondDate = df.parse(endtime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
        return nDay;
    }

    //比较传入的字符串日期跟当前系统日期的关系
    public static int compareTo(String str) {
        //int(n>0 在当前日期之后，n<0 在当前日期之前，n=0等于当前日期。)
        int isAfterOrBeforeDate = str2Date(str).compareTo(str2Date(getDateTime()));
        return isAfterOrBeforeDate;
    }

}